package com.huangd.platform.modules.sys.controller;

import com.huangd.platform.config.GlobalConfig;
import com.huangd.platform.modules.sys.entity.to.GetFileListTo;
import com.huangd.platform.modules.sys.service.SysFileService;
import com.huangd.platform.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * 文件管理
 *
 * @author huangd
 */
@RestController
@Api(tags = {"文件相关"})
@RequestMapping("/sys/file")
public class SysFileController extends AbstractController {
    @Resource
    private SysFileService fileService;

    @PostMapping("/list")
    @ApiOperation(value = "文件列表")
    public R list(HttpServletResponse response, @RequestBody(required = false) GetFileListTo getFileListTo) {
        String uri = getFileListTo == null ? "" : getFileListTo.getUri();
        String sort_by = getFileListTo == null ? "" : getFileListTo.getSort_by();
        String sort = getFileListTo == null ? "" : getFileListTo.getSort();


        return fileService.list(response, uri, sort_by, sort);
    }

    @GetMapping("createdir")
    @ApiOperation(value = "创建目录")
    public R createdir(@RequestParam("uri") String uri, @RequestParam("newdir") String newdir) throws Exception {

        String dir = GlobalConfig.DATA_FILE_SYSTEM;
        File file = new File(dir, uri);
        File createFile = new File(file, newdir);

        //已经存在
        if (!createFile.exists())
            createFile.mkdirs();
        return R.ok();
    }

    @PostMapping("upload")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uri", value = "上传文件目录", required = true),
            @ApiImplicitParam(name = "incType", value = "解压文件:1", required = true),
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class)
    })
    public R upload(@RequestPart @RequestParam("file") MultipartFile file, @RequestParam(value = "uri") String uri,
                    @RequestParam("incType") int incType) {

        return fileService.upload(file, uri, incType);
    }

    @GetMapping("clear")
    @ApiOperation(value = "清空目录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uri", value = "文件目录", required = true),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true)
    })
    public R clear(@RequestParam String uri, @RequestParam String path) {

        return fileService.clear(uri, path);

    }

    @GetMapping("delete")
    @ApiOperation(value = "删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path", value = "文件路径", required = true)
    })
    public R delete(@RequestParam String path) {

        return fileService.delete(path);

    }

}
